package com.maket.jbxx.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.maket.jbxx.domain.CpppProductBrand;
import com.maket.jbxx.domain.KsMerchants;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.service.ICpppProductBrandService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 产品品牌Controller
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/brand")
public class CpppProductBrandController extends BaseController
{
    @Autowired
    private ICpppProductBrandService cpppProductBrandService;

    @Resource
    private CodeClient codeClient;
    /**
     * 查询产品品牌列表
     */
    @RequiresPermissions("jbxx:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(CpppProductBrand cpppProductBrand)
    {
        startPage();
        List<CpppProductBrand> list = cpppProductBrandService.selectCpppProductBrandList(cpppProductBrand);
        return getDataTable(list);
    }

    /**
     * 导出产品品牌列表
     */
    @RequiresPermissions("jbxx:brand:export")
    @Log(title = "产品品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpppProductBrand cpppProductBrand)
    {
        List<CpppProductBrand> list = cpppProductBrandService.selectCpppProductBrandList(cpppProductBrand);
        ExcelUtil<CpppProductBrand> util = new ExcelUtil<CpppProductBrand>(CpppProductBrand.class);
        util.exportExcel(response, list, "产品品牌数据");
    }

    /**
     * 获取产品品牌详细信息
     */
    @RequiresPermissions("jbxx:brand:query")
    @GetMapping(value = "/{cptypeCode}")
    public AjaxResult getInfo(@PathVariable("cptypeCode") String cptypeCode)
    {
        return success(cpppProductBrandService.selectCpppProductBrandByCptypeCode(cptypeCode));
    }

    /**
     * 新增产品品牌
     */
    @RequiresPermissions("jbxx:brand:add")
    @Log(title = "产品品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpppProductBrand cpppProductBrand)
    {
        cpppProductBrand.setCptypeCode(codeClient.getCode(10));
        cpppProductBrand.setIsUse(SecurityUtils.getUserId());

        cpppProductBrand.getKsMerchantsList().forEach(item->cpppProductBrand.setKsCode(item.getKsCode()));

        return toAjax(cpppProductBrandService.insertCpppProductBrand(cpppProductBrand));
    }

    /**
     * 修改产品品牌
     */
    @RequiresPermissions("jbxx:brand:edit")
    @Log(title = "产品品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpppProductBrand cpppProductBrand)
    {

        cpppProductBrand.getKsMerchantsList().forEach(k->cpppProductBrand.setKsCode(k.getKsCode()));

        return toAjax(cpppProductBrandService.updateCpppProductBrand(cpppProductBrand));
    }


    /**
     * 修改产品品牌状态
     */
    @RequiresPermissions("jbxx:merchants:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping("isStatus")
    public AjaxResult editIsStart(@RequestBody CpppProductBrand cpppProductBrand)
    {
        return toAjax(cpppProductBrandService.updateCpppProductBrandIsStatus(cpppProductBrand));
    }


    /**
     * 删除产品品牌
     */
    @RequiresPermissions("jbxx:brand:remove")
    @Log(title = "产品品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cptypeCodes}")
    public AjaxResult remove(@PathVariable String[] cptypeCodes)
    {
        return toAjax(cpppProductBrandService.deleteCpppProductBrandByCptypeCodes(cptypeCodes));
    }
}
